﻿using System;
using System.Collections.Generic;
using System.Text;
using DirectShowLib;

namespace MulticastTV.DirectShow
{

    class DShow
    {
        private const int WM_GRAPHNOTIFY = 0x4000 + 88;

        // DirectShow objects
        private IFilterGraph2 graphBuilder;
        private DsROTEntry rot;
        IntPtr Handle;

        private IBaseFilter dsnet;
        private IBaseFilter psipars;

        public DShow(IntPtr handle)
        {
            Handle = handle;
        }

        public void Init()
        {
            int hr = 0;

            // Create a DirectShow FilterGraph
            graphBuilder = (IFilterGraph2)new FilterGraph();

            // Add it in ROT for debug purpose
            rot = new DsROTEntry(graphBuilder);

            // Add a notification handler (see WndProc)
            hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
            DsError.ThrowExceptionForHR(hr);
        }

        public void RenderFile(string filename)
        {
            // Render the file
            int hr = graphBuilder.RenderFile(filename, null);
            DsError.ThrowExceptionForHR(hr);
        }

        public void RunGraph()
        {
            // Run the graph
            int hr = (graphBuilder as IMediaControl).Run();
            DsError.ThrowExceptionForHR(hr);
        }

        public void AddDsNetworkFilter()
        {
            dsnet = (IBaseFilter) new DsNetReceive();
        
        }

        static public uint IPAddressToLong(string IPAddr)
        {
            System.Net.IPAddress oIP = System.Net.IPAddress.Parse(IPAddr);
            byte[] byteIP = oIP.GetAddressBytes();

            uint ip = (uint)byteIP[3] << 24;
            ip += (uint)byteIP[2] << 16;
            ip += (uint)byteIP[1] << 8;
            ip += (uint)byteIP[0];

            return ip;
        }

    }
}
